package com.rental.web.service.impl;

import com.rental.common.exception.ProjectException;
import com.rental.common.result.ResultCodeEnum;
import com.rental.common.util.AjaxResultHandler;
import com.rental.houses.client.RentalHourseFeign;
import com.rental.houses.client.RequiredHouseFegin;
import com.rental.houses.client.SubletHouseFeignClient;
import com.rental.model.entity.user.UserInfo;
import com.rental.model.form.user.GetRecordForm;
import com.rental.model.form.user.PhoneAndCodeForm;
import com.rental.model.vo.user.RecordVo;
import com.rental.user.client.UserInfoFeignClient;
import com.rental.web.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private RentalHourseFeign rentalHourseFeign;

    @Autowired
    private RequiredHouseFegin requiredHouseFegin;

    @Autowired
    private SubletHouseFeignClient subletHouseFeignClient;


    @Override
    public String sendCode(PhoneAndCodeForm phoneAndCodeForm) {
        if (!phoneAndCodeForm.getPhone().matches("^1[3-9]\\d{9}$")) throw new ProjectException(ResultCodeEnum.PHONE_ERROR);
        return AjaxResultHandler.handle(userInfoFeignClient.sendCode(phoneAndCodeForm));
    }

    @Override
    public String login(PhoneAndCodeForm phoneAndCodeForm) {
        if (!phoneAndCodeForm.getPhone().matches("^1[3-9]\\d{9}$")) throw new ProjectException(ResultCodeEnum.PHONE_ERROR);
        if (StringUtils.isBlank(phoneAndCodeForm.getCode()) || phoneAndCodeForm.getCode().length() != 4) throw new ProjectException(ResultCodeEnum.CODE_ERROR);
        return AjaxResultHandler.handle(userInfoFeignClient.login(phoneAndCodeForm));
    }

    @Override
    public UserInfo getUserInfo(Long userId) {
        return AjaxResultHandler.handle(userInfoFeignClient.getUserInfoById(userId));
    }

    @Override
    public RecordVo getMyRelease(GetRecordForm getRecordForm) {
        return AjaxResultHandler.handle(userInfoFeignClient.getPublish(getRecordForm));
    }

    @Override
    public UserInfo getUserById(Long id) {
        return userInfoFeignClient.getUserById(id).getData();
    }

    /**
     * 获取浏览记录
     *
     * @param getRecordForm 请求表单
     * @return 浏览记录
     */
    @Override
    public RecordVo getBrowseRecord(GetRecordForm getRecordForm) {
        return AjaxResultHandler.handle(userInfoFeignClient.getBrowseRecord(getRecordForm));
    }

    /**
     * 获取收藏信息
     *
     * @param getRecordForm 请求表单
     * @return 收藏信息
     */
    @Override
    public RecordVo getCollectInfo(GetRecordForm getRecordForm) {
        return AjaxResultHandler.handle(userInfoFeignClient.getCollectInfo(getRecordForm));
    }

    @Override
    public List<UserInfo> getAllUserInfo() {
        return userInfoFeignClient.getAllUserInfo().getData();
    }
}
